home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swags_z.zip / STRINGS.SWG / 0083_Clean String.pas < prev    next >
Pascal/Delphi Source File  |  1994-08-24  |  1KB  |  51 lines

  1. function Str2Int(Str:string): integer;
  2. var
  3.   temp,code : integer;
  4. begin
  5.   if length(Str) = 0 then
  6.      Str2Int := 0
  7.   else begin
  8.     val(Str,temp,code);
  9.     if code = 0 then
  10.        Str2Int := temp
  11.     else
  12.        Str2Int := 0;
  13.   end;
  14. end;
  15.  
  16. function StripFrontChars(Var S : String;Ch : Char) : String;
  17. var
  18.   S1 : String;
  19. begin
  20.   While (S[1] = Ch) and (Length(S) > 0) do
  21.   S := Copy(S,2,Length(S) - 1);
  22.   StripFrontChars := S
  23. end;
  24.  
  25. function StripBlanks(Var S : String) : String;
  26. var
  27.   i : Integer;
  28. begin
  29.   i := Length(S);
  30.   while S[i] = ' ' do begin
  31.     Delete(S,i,1);
  32.     Dec(i);
  33.   end;
  34.   StripBlanks := S;
  35. end;
  36.  
  37. function CleanString(var S: String): String;
  38. begin
  39.   StripFrontChars(S, #32);
  40.   StripBlanks(S);
  41. end;
  42.  
  43. var
  44.   S: String;
  45.   i: Integer;
  46. begin
  47.   S := '   3   ';        { Create a bad string that will cause errors }
  48.   CleanString(S);        { Clean it up                                }
  49.   i := Str2Int(S);       { Convert                                    }
  50.   WriteLn(i);            { Show it to the screen                      }
  51. end.